package com.demo.aty;

import com.airbnb.lottie.Lottie;
import com.binioter.guideview.Guide;
import com.binioter.guideview.GuideBuilder;
import com.binioter.guideview.viewComment;
import com.demo.aty.component.LottieComponent;
import com.demo.aty.component.MutiComponent;
import com.demo.aty.component.SimpleComponent;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.StackLayout;
import ohos.agp.components.element.FrameAnimationElement;
import ohos.agp.utils.LayoutAlignment;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

public class SimpleGuideViewSlice extends AbilitySlice {
    private Component btn1;
    private ComponentContainer rootView;
    private EventHandler eventHandler;
    private Component nearbyView;
    private Component liveRoomView;
    private Component component;
    private FrameAnimationElement frameAnimationElement;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_simple_guide_view);
        btn1 = findComponentById(ResourceTable.Id_btn1);
        rootView = (ComponentContainer) findComponentById(ResourceTable.Id_root);
        nearbyView = findComponentById(ResourceTable.Id_nearby_view);
        liveRoomView = findComponentById(ResourceTable.Id_liveRoom_view);
        eventHandler = new EventHandler(EventRunner.getMainEventRunner());
        eventHandler.postTask(new Runnable() {
            @Override
            public void run() {
                initView();
            }
        }, 100);
    }

    private void initView() {
        GuideBuilder builder = new GuideBuilder();
        builder.setTargetView(btn1)
                .setAlpha(155)
                .setHighTargetCorner(20)
                .setHighTargetPadding(10);
        builder.setOnVisibilityChangedListener(new GuideBuilder.OnVisibilityChangedListener() {
            @Override
            public void onShown() {
            }

            @Override
            public void onDismiss() {
                showViewTwo();
            }
        });

        builder.addComponent(new SimpleComponent());
        Guide guide = builder.createGuide();
        guide.show(SimpleGuideViewSlice.this, rootView, ResourceTable.Media_arrow, "添加好友在这里哦");
    }

    private void showViewTwo() {
        GuideBuilder builder = new GuideBuilder();
        builder.setTargetView(nearbyView)
                .setAlpha(155)
                .setHighTargetCorner(20)
                .setHighTargetGraphStyle(viewComment.CIRCLE)
                .setHighTargetPadding(10);
        builder.setOnVisibilityChangedListener(new GuideBuilder.OnVisibilityChangedListener() {
            @Override
            public void onShown() {
            }

            @Override
            public void onDismiss() {
                showViewThree();
            }
        });
        builder.addComponent(new MutiComponent());
        Guide guide = builder.createGuide();
        guide.show(SimpleGuideViewSlice.this, rootView, ResourceTable.Media_arrow, "查看附近的人");
    }

    private void showViewThree() {
        GuideBuilder builder = new GuideBuilder();
        builder.setTargetView(liveRoomView)
                .setAlpha(155)
                .setHighTargetCorner(20)
                .setHighTargetPadding(10);

        builder.setOnVisibilityChangedListener(new GuideBuilder.OnVisibilityChangedListener() {
            @Override
            public void onShown() {
                frameAnimationElement = Lottie.getFrameAnimationElement(getContext());
                frameAnimationElement.setOneShot(false);
                component = new StackLayout(getContext());
                component.setBackground(frameAnimationElement);
                StackLayout.LayoutConfig layoutConfig = new StackLayout.LayoutConfig();
                layoutConfig.width = 700;
                layoutConfig.height = 1516;
                layoutConfig.alignment = LayoutAlignment.CENTER;
                rootView.addComponent(component, layoutConfig);
                frameAnimationElement.start();
            }

            @Override
            public void onDismiss() {
                frameAnimationElement.stop();
                rootView.removeComponent(component);

            }
        });
        builder.addComponent(new LottieComponent());
        Guide guide = builder.createGuide();
        guide.show(SimpleGuideViewSlice.this, rootView, 0, "上滑查看更多");
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
